<?php

namespace App\Http\Resources;

use App\Models\Goods;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

/** @mixin Goods */
class GoodsResource extends JsonResource
{
    /**
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        $data = parent::toArray($request);
        $data['img'] = strpos($data['img'],'https') === false ? sprintf('%s/%s', aliyun_domain(), $data['img']) : $data['img'];
        //$data['detail_url'] = env('APP_URL').'/goods/'.$data['id'];
        //$data['discount_rate'] = isset($data['condition']) ?(string)$data['discount_rate']:"0";

        if(isset($data['price']) && $data['profit']){
            $data['old_price'] = $data['price'] + $data['profit'];
        }

        if(!empty($data['imgs'])) {
            foreach ($data['imgs'] as $key=>$value) {
                $data['imgs'][$key] = strpos($value,'https') === false ? sprintf('%s/%s', aliyun_domain(), $value) : $value;
            }
        }
        $data['virtual_sales'] = intval($data['quality'] / 3.4);
        $data['like'] = intval($data['quality'] * 3.9);

        return $data;
    }
}
